﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain;
using RBAC.Domain.Dto;
using RBAC.Server.Interface;
using Swork03_28.Api.Application.Commands.RoleCommand;
using Swork03_28.Domain.RBAC;

namespace RBAC.Poject.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RolesController : ControllerBase
    {
        private readonly IRoleServer _roleServer;
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="roleServer"></param>
        public RolesController(IRoleServer roleServer, IMediator mediator)
        {
            _roleServer = roleServer;
            _mediator = mediator;
        }


        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>受影响的行数</returns>
        [HttpPost]
        public async Task<int> CreateRole(RoleCreateCommand role)
        {
            return await _mediator.Send(role);
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <returns>角色信息</returns>
        [HttpGet]
        public async Task<IActionResult> GetRoles([FromQuery]RoleSelectCommand command)
        {
            var data = await _mediator.Send(command);
            return Ok(new
            {
                data.totalCount,
                data.pageCount,
                data.list
            });
        }

        /// <summary>
        /// 获取用户树状
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<MenuItem>> GetMenuItem(int SectorId)
        {
            return await _roleServer.GetMenuItem(SectorId);
        }

        /// <summary>
        /// 逻辑删除角色信息
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <returns>状态码</returns>
        [HttpPut]
        public async Task<int> Del(int id)
        {
            return await _roleServer.Del(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <returns>角色信息</returns>
        [HttpGet]
        public async Task<Roles> GetRolesUpd(int id)
        {
            return await _roleServer.GetRolesUpd(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>状态码</returns>
        [HttpPut]
        public Task<int> Update(Roles role)
        {
            return _roleServer.Update(role);
        }


        /// <summary>
        /// 给员工添加角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        /// <param name="SectorId"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<int> AddRole(List<MenuItem> userId, int roleId, int SectorId)
        {
            return _roleServer.AddRole(userId, roleId, SectorId);
        }

        /// <summary>
        /// 获取某个角色下的员工信息
        /// </summary>
        /// <param name="roleId">角色编号</param>
        /// <returns>符合条件的数据</returns>
        [HttpGet]
        public Task<List<UserDto>> GetUserInfos(int roleId)
        {
            return _roleServer.GetUserInfos(roleId);
        }

        /// <summary>
        /// 逻辑删除角色下的用户
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<int> DelRoleUser(int roleId, int userId)
        {
            return _roleServer.DelRoleUser(roleId, userId);
        }

        /// <summary>
        /// 根据角色编号找到角色下的用户信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<List<MenuItem>> UpdUserRole(int RoleId)
        {
            return _roleServer.UpdUserRole(RoleId);
        }

        /// <summary>
        /// 根据部门找到用户然后回填数据
        /// </summary>
        /// <param name="dId"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<List<MenuItem>> UpdUserRoleAsDoor(int dId)
        {
            return _roleServer.UpdUserRoleAsDoor(dId);
        }
    }
}
